## Check for gem prerequisites
%w{colored}.each do |component|
  unless Gem.available?(component)
    run "gem install #{component} -q"
    Gem.refresh
    Gem.activate(component)
  end
end

require 'colored'
puts "\n    All required gems installed\n".yellow

if yes? "  Use RVM? [yN]".cyan
  puts "\n    Applying RVM specific changes".yellow
  apply File.join File.dirname(__FILE__), "rvm.rb"
end

puts "\n    Applying gems specific changes".yellow
apply File.join File.dirname(__FILE__), "gems.rb"

puts "\n    Running bundle install".yellow
run "bundle install > /dev/null"

puts "\n    Applying application specific changes".yellow
apply File.join File.dirname(__FILE__), "application.rb"

puts "\n    Applying HAML and SASS specific changes".yellow
apply File.join File.dirname(__FILE__), "haml_and_sass.rb"

puts "\n    Applying JQuery specific changes".yellow
apply File.join File.dirname(__FILE__), "jquery.rb"

puts "\n    Applying Formtastic specific changes".yellow
apply File.join File.dirname(__FILE__), "formtastic.rb"

puts "\n    Applying RSpec specific changes".yellow
apply File.join File.dirname(__FILE__), "rspec.rb"

puts "\n    Applying FactoryGirl specific changes".yellow
apply File.join File.dirname(__FILE__), "factory_girl.rb"

inject_into_file('config/application.rb', :after => "config.filter_parameters += [:password, :password_confirmation]") do
  %q{
    config.generators do |g|
      g.stylesheets false
      g.form_builder :formtastic
      g.template_engine :haml
      g.test_framework :rspec, :fixture => true, :views => false
      g.integration_tool :rspec, :fixture => true, :views => true
      g.fixture_replacement :factory_girl, :dir => "spec/factories"
    end

    # Global Sass Option
    Sass::Plugin.options[:template_location] = { 'app/stylesheets' => 'public/stylesheets' }
  }
end

puts "\n    Applying Rails 3 Generators specific changes".yellow
apply File.join File.dirname(__FILE__), "rails3_generators.rb"

puts "\n    Applying Devise, Warden and Cancan specific changes".yellow
apply File.join File.dirname(__FILE__), "authentication.rb"

puts "\n    Applying Nifty Generators specific changes".yellow
apply File.join File.dirname(__FILE__), "nifty_generators.rb"

puts "\n    Applying Will Paginate specific changes".yellow
apply File.join File.dirname(__FILE__), "will_paginate.rb"

puts "\n    Applying Auotest specific changes".yellow
apply File.join File.dirname(__FILE__), "autotest.rb"

puts "\n    Applying Jasmine specific changes".yellow
apply File.join File.dirname(__FILE__), "jasmine.rb"

puts "\n    Applying Spork specific changes".yellow
apply File.join File.dirname(__FILE__), "spork.rb"

puts "\n    Applying Jammit specific changes".yellow
apply File.join File.dirname(__FILE__), "jammit.rb"

puts "\n    Applying Faker specific changes".yellow
apply File.join File.dirname(__FILE__), "faker.rb"

puts "\n    Applying Welcome Controller specific changes".yellow
apply File.join File.dirname(__FILE__), "welcome.rb"

puts "\n    Clearing default index".yellow
remove_file "public/index.html"
remove_file "public/images/rails.png"

puts "\n    Applying Javascript specific changes".yellow
apply File.join File.dirname(__FILE__), "javascript.rb"

puts "\n    Applying Capistrano specific changes".yellow
apply File.join File.dirname(__FILE__), "capistrano.rb"

puts "\n    Applying Compass specific changes".yellow
apply File.join File.dirname(__FILE__), "compass.rb"

puts "\n    Applying Friendly ID specific changes".yellow
apply File.join File.dirname(__FILE__), "friendly_id.rb"

puts "\n    Applying Git specific changes".yellow
apply File.join File.dirname(__FILE__), "git.rb"

puts "\n    Runing all migrations".yellow
run "rake db:migrate"